import axios from 'axios';
import Nprogress from 'nprogress';
import { getToken } from '@/utils/auth';


export const request = axios.create({
  baseURL: '/api',
  timeout: 10000,
});

request.interceptors.request.use((config) => {
  Nprogress.start();
  const token = getToken();
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});

request.interceptors.response.use((response) => {
  Nprogress.done();
  if (!response.data.success) return Promise.reject(new Error(response.data.message));
  return response.data;
}, (error) => {
  Nprogress.done();
  return Promise.reject(error);
});


export default request;
